# Definition for singly-linked list.
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution:
	# @param {ListNode} head
	# @param {integer} k
	# @return {ListNode}
	def reverseKGroup(self, head, k):
		if k <= 1:
			return head
		size = 0
		it = head
		while it:
			size += 1
			it = it.next
		g = size / k
		H = ListNode(None)
		H.next = head
		h = H
		tmp = h.next
		for x in xrange(g):
			if x > 0:
				for i in xrange(k):
					h = h.next
			FirstNode = h.next
			for j in xrange(k):
				curr = tmp
				tmp = tmp.next
				curr.next = h.next
				h.next = curr
				if j == k-1:
					FirstNode.next = tmp
		return H.next

if __name__ == '__main__':
    h = ListNode(1)
    '''
    it = h
    for i in [2,3,4,5,6,7]:
        it.next = ListNode(i)
        it = it.next
	'''
    so = Solution()
    
    it2 = so.reverseKGroup(h,1)
    while it2 is not None:
        print it2.val
        it2 = it2.next